package com.jueling.culture.service.impl;


import com.jueling.culture.core.AbstractService;
import com.jueling.culture.dto.req.RoleInfoDto;
import com.jueling.culture.exception.FrontShowException;
import com.jueling.culture.mapper.RoleAuthorityMapper;
import com.jueling.culture.mapper.RoleInfoMapper;
import com.jueling.culture.mapper.UserInfoMapper;
import com.jueling.culture.model.RoleAuthority;
import com.jueling.culture.model.RoleEditor;
import com.jueling.culture.model.RoleInfo;
import com.jueling.culture.service.RoleEditorService;
import com.jueling.culture.service.RoleInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author linp
 * @date  2021/03/09
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RoleInfoServiceImpl extends AbstractService<RoleInfo> implements RoleInfoService {

    @Resource
    private RoleInfoMapper roleInfoMapper;

    @Resource
    private RoleAuthorityMapper roleAuthorityMapper;

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private RoleEditorService roleEditorService;


    /**
     * 获取角色列表
     * @return
     */
    @Override
    public List<RoleInfo> getRoleInfoList() {
        return roleInfoMapper.getRoleInfoList();
    }

    /**
     * 根据ID获取角色详情
     * @param roleId
     * @return
     */
    @Override
    public RoleInfo getRoleInfoById(String roleId) {
        return roleInfoMapper.getRoleInfoById(roleId);
    }

    /**
     * 编辑角色
     * @param roleId
     * @param roleInfoDto
     */
    @Override
    public void editRoleInfo(String roleId, RoleInfoDto roleInfoDto) {
        //查询该角色的角色名
        String roleName = roleInfoMapper.getRoleName(roleId);
        //查询角色是否存在
        Integer roleCount= roleInfoMapper.getRoleCount(roleInfoDto.getRoleName());
        if(roleCount>0&&!roleInfoDto.getRoleName().equals(roleName)){
            throw new FrontShowException("角色名已存在");
        }
        //删除角色权限
        roleAuthorityMapper.deleteRoleAuthority(roleId);
        //新增角色权限
        List<RoleAuthority> roleAuthorityList=roleInfoDto.getRoleAuthority();
        if(roleAuthorityList!=null&&!roleAuthorityList.isEmpty()){
            roleAuthorityMapper.addRoleAuthority(roleId,roleAuthorityList);
        }
        //编辑角色
        roleInfoMapper.editRoleInfo(roleId, roleInfoDto);
    }

    /**
     * 新增角色
     * @param roleInfoDto
     */
    @Override
    public void addRoleInfo(RoleInfoDto roleInfoDto) {
        //查询角色是否存在
        Integer roleCount= roleInfoMapper.getRoleCount(roleInfoDto.getRoleName());
        if(roleCount>0){
            throw new FrontShowException("新增角色失败");
        }
        //新增角色
        roleInfoMapper.addRoleInfo(roleInfoDto);
        //新增角色权限
        List<RoleAuthority> roleAuthorityList=roleInfoDto.getRoleAuthority();
        if(roleAuthorityList!=null&&!roleAuthorityList.isEmpty()){
            roleAuthorityMapper.addRoleAuthority(roleInfoDto.getRoleId(),roleAuthorityList);
        }
        //新增角色编辑权限
        RoleEditor roleEditor = new RoleEditor();
        roleEditor.setRoleId(Integer.valueOf(roleInfoDto.getRoleId()));
        roleEditorService.save(roleEditor);

    }
    @Override
    public void deleteRoleById(String roleId) {
        //查询是否有用户绑定该角色
        Integer userCount = userInfoMapper.userCount(roleId);
        if (userCount!=0){
            throw new FrontShowException("该角色模板被其他系统管理账号应用，不可删除");
        }
        //删除角色权限
        roleAuthorityMapper.deleteRoleAuthority(roleId);
        this.deleteByIds(roleId);
    }

    /**
     * 判断角色名是否存在(true-不存在，false-存在)
     * @param roleName
     * @return
     */
    @Override
    public boolean checkRole(String roleName) {
        boolean check;
        Integer roleCount= roleInfoMapper.getRoleCount(roleName);
        if(roleCount==0){
            check=true;
        }else {
            check=false;
        }
        return check;
    }

}
